package com.shushan.mixedexam.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.shushan.base.BaseActivity;
import com.shushan.mixedexam.bean.ExerciseVideoBean;
import com.shushan.pages.SpecialTopicVideoActivity;
import com.shushan.shushanbao.R;
import com.shushan.util.ImageUtils;
import com.shushan.util.VideoUtils;

import java.util.List;


/**
 * @ClassName: VideoPointsAdapter
 * @Desciption: 知识点视频讲解Adapter
 * @author: yichaohua
 * @date: 2018-01-06
 */
public class VideoPointsAdapter extends BaseAdapter{

    private static final String TAG = "VideoPointsAdapter";

    private BaseActivity context;
    private List<ExerciseVideoBean> videoPointBeanList;

    public VideoPointsAdapter(BaseActivity context, List<ExerciseVideoBean> videoPointBeanList) {
        this.context = context;
        this.videoPointBeanList = videoPointBeanList;
    }

    @Override
    public int getCount() {
        return videoPointBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return videoPointBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            //填充布局
            convertView =  LayoutInflater.from(context).inflate(R.layout.video_points_list_item, parent,false);
            holder = new ViewHolder();
            //通过ID查询控件
            holder.imageView = convertView.findViewById(R.id.video_points_title_img);
            holder.titleTextView = convertView.findViewById(R.id.video_points_title_tv);
            holder.desTextView = convertView.findViewById(R.id.video_points_des_tv);
            holder.playButton = convertView.findViewById(R.id.video_points_play_btn);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        final ExerciseVideoBean videoPointBean = videoPointBeanList.get(position);
        holder.titleTextView.setText(videoPointBean.video_title);
        holder.desTextView.setText(videoPointBean.video_desc);
        ImageUtils.loadImageView(context,videoPointBean.cover,holder.imageView);
        holder.playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, SpecialTopicVideoActivity.class);
                intent.putExtra("isOnlyPlay",true);
                intent.putExtra("video_title",videoPointBean.video_title);
                intent.putExtra("video_url",videoPointBean.video_url);
                context.startActivityAllow(intent);
//                VideoUtils.playVideo(context,videoPointBean.video_url);
            }
        });

        return convertView;
    }

    public static class ViewHolder{
        ImageView imageView;
        TextView titleTextView;
        TextView desTextView;
        Button playButton;
    }
}
